package com.sdrongyou.tms.service.impl;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sdrongyou.tms.dto.CarDO;
import com.sdrongyou.tms.dto.CarVO;
import com.sdrongyou.tms.dto.DriverVO;
import com.sdrongyou.tms.mapper.CarMapper;
import com.sdrongyou.tms.service.CarIService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CarIServiceImpl implements CarIService {

    @Resource
    private CarMapper carMapper;

    @Resource
    private BeanSearcher beanSearcher;

    /**
     * 新增车辆信息
     */
    @Override
    public int saveCarInfo(CarDO carDO) {
        //把绑定司机的数组转换成字符串
        String bindDriverStr = String.join(",", carDO.getBindDriverArray());
        carDO.setBindDriver(bindDriverStr);
        return carMapper.insert(carDO);
    }

    /**
     * 修改车辆信息
     */
    @Override
    public int updateCarInfo(CarDO carDO) {
        String bindDriverStr = String.join(",", carDO.getBindDriverArray());
        carDO.setBindDriver(bindDriverStr);
        return carMapper.updateById(carDO);
    }

    /**
     * 删除车辆信息
     */
    @Override
    public int deleteCarInfo(Long id) {
        return carMapper.deleteById(id);
    }

    /**
     * 批量删除车辆信息
     */
    @Override
    public int batchDeleteCarInfo(List<Long> ids) {
        return carMapper.deleteByIds(ids);
    }

    /**
     * 分页查询
     */
    @Override
    public List<CarVO> queryCarDataPageList(CarDO carDO) {
        Map<String,Object> params = MapUtils.builder()
                .field("car_number", carDO.getCarNumber())
                .op("ct")
                .build();
        List<CarVO> carVOList = beanSearcher.searchList(CarVO.class, params);

        // 根据绑定司机的id查询对应的司机姓名
        carVOList.stream().forEach(carVO -> {
            if (StringUtils.isNotEmpty(carVO.getBindDriver())) {
                // 将逗号分隔的ID字符串转换为List
                List<Long> driverIds = Arrays.stream(carVO.getBindDriver().split(","))
                        .map(String::trim)
                        .filter(StringUtils::isNotEmpty)
                        .map(Long::valueOf)
                        .collect(Collectors.toList());

                if (!driverIds.isEmpty()) {
                    Map<String,Object> pm = MapUtils.builder()
                            .field("id", driverIds)
                            .op("il")
                            .build();
                    // 查询司机列表
                    List<DriverVO> driverVOList = beanSearcher.searchList(DriverVO.class, pm);
                    // 使用逗号分隔司机姓名
                    String driverNames = driverVOList.stream()
                            .map(DriverVO::getName)
                            .collect(Collectors.joining(","));
                    carVO.setBindDriverName(driverNames);
                }
            }

        });

        return carVOList;
    }

    /**
     * 查询单条车辆信息
     */
    @Override
    public CarVO queryCarById(Long id) {
        Map<String,Object> params = MapUtils.builder()
                .field("id", id)
                .build();
        return beanSearcher.searchFirst(CarVO.class, params);

    }

}
